package com.taobao.user.controller;
import com.taobao.basic.exception.BusinessException;
import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.PageList;
import com.taobao.user.domain.User;
import com.taobao.user.dto.RegisterDto;
import com.taobao.user.query.UserQuery;
import com.taobao.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/user")
@Api(value = "员工接口类",description = "员工接口的CRUD操作")
public class UserController {

    @Autowired
    private IUserService UserService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<User> findAll(){
        return UserService.loadAll();
    }
    /**
     * 根据id查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public User findOne(@PathVariable("id") Long id){
        return UserService.loadById(id);
    };
    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public JsonResult DeleteByID(@PathVariable("id") Long id){
        try {
            UserService.remove(id);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 添加或修改操作
     * @param User
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public JsonResult updateOrAdd(@RequestBody User User ){
        try {
            if (User.getId() == null){
                UserService.save(User);
            }else {
                UserService.update(User);
            }
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     *
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给UserQuery
    public PageList<User> pageQuery(@RequestBody UserQuery query){
        try {
            return UserService.pageQuery(query);
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<User>();
        }
    }
    /**
     * 批量删除
     * @param
     * @return
    */
    @PatchMapping()
    @ApiOperation(value = "删除接口")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            UserService.patchDel(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 手机验证码注册接口
     * @param dto
     * @return
     */
    @PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            UserService.phoneRegister(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常，稍后重试！");
        }
    }

}
